package com.example.hdwx.myapplication.HomeActivity.daydaycook_fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.hdwx.myapplication.R;
import com.example.hdwx.myapplication.adapter.Daydaycook_adapter;
import com.example.hdwx.myapplication.eneity.Api;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_everyday_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_hot_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_raiders_eneity;
import com.example.hdwx.myapplication.json.Daydaycook_json;
import com.example.hdwx.myapplication.untils.FullyLinearLayoutManager;
import com.example.hdwx.myapplication.untils.MyScrollView;
import com.example.hdwx.myapplication.untils.PullDownElasticImp;
import com.example.hdwx.myapplication.untils.PullDownScrollView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/12.
 */

public class Daydaycook_Fragment extends Fragment implements PullDownScrollView.RefreshListener {
    private PullDownScrollView mPullDownScrollView;
    String TAG ="Daydaycook_Fragment";
    List <Daydaycook_everyday_eneity>list1=new ArrayList<>();
    List<Daydaycook_hot_eneity> list2=new ArrayList<>();
    List<Daydaycook_raiders_eneity> list3=new ArrayList<>();
    List<Daydaycook_eneity> list=new ArrayList<>();
    RecyclerView  recycler_view;
    RecyclerView.Adapter adapter=null;
    MyScrollView refreshLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.daydaycook_fragment,container,false );
        recycler_view= (RecyclerView) view.findViewById(R.id.daydaycook_recyclview);
        mPullDownScrollView = (PullDownScrollView) view.findViewById(R.id.refresh_root);
        mPullDownScrollView.setRefreshListener(this);
        mPullDownScrollView.setPullDownElastic(new PullDownElasticImp(getContext()));
        recycler_view.setNestedScrollingEnabled(true);
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(getContext());
        recycler_view.setLayoutManager(linearLayoutManager);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
     /*   refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新时删除数据库，重新获取数据

                DataSupport.deleteAll(Daydaycook_eneity.class);
                DataSupport.deleteAll(Daydaycook_hot_eneity.class);
                DataSupport.deleteAll(Daydaycook_everyday_eneity.class);
                DataSupport.deleteAll(Daydaycook_raiders_eneity.class);
                getData();
            }
        });*/
    }

    //获取数据
    public void getData() {
        //先判断数据库是否有数据，没有就请求数据
        DataSupport.findAll(Daydaycook_eneity.class);
        DataSupport.findAll(Daydaycook_hot_eneity.class);
        DataSupport.findAll(Daydaycook_everyday_eneity.class);
        DataSupport.findAll(Daydaycook_raiders_eneity.class);
        if(list.size()!=0||list1.size()!=0||list2.size()!=0||list3.size()!=0){
            recycler_view.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
            recycler_view.setAdapter(new Daydaycook_adapter(list,list1,list2,list3,getContext()));
        }else {
            //获取顶部图片轮播的数据
            getData0();
        }

    }
//获取每日新菜馆的数据
    public void getData1() {
        OkHttpUtils.get().url(new Api().getSEARCH_PATH()).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Log.e(TAG,"每日新菜馆获取数据失败");
            }
            @Override
            public void onResponse(String response) {
               list1 = new Daydaycook_json().everyday_json(response);

                //获取当红人气菜的数据
                getData2();
            }
        });
    }
    //获取当红人气菜的数据
    public void getData2() {
        OkHttpUtils.get().url(new Api().getRECOMMENDdAll_PATH()).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Log.e(TAG,"当红人气菜和美食全攻略获取数据失败");
            }
            @Override
            public void onResponse(String response) {
                list2 = new Daydaycook_json().hot_json(response);
                list3=new Daydaycook_json().raiders_json(response);
                recycler_view.setAdapter(new Daydaycook_adapter(list,list1,list2,list3,getContext()));

            }
        });
    }
    //获取顶部图片轮播的数据
    public void getData0() {
        OkHttpUtils.get().url(new Api().getIMAGES()).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Log.e(TAG,"图片轮播获取数据失败");
            }
            @Override
            public void onResponse(String response) {
                list = new Daydaycook_json().image_json(response);
                //获取每日新菜馆的数据
                getData1();
             //   Log.e(TAG,"onBindViewHolder运行了"+list.size() );
            }
        });
    }

    @Override
    public void onRefresh(PullDownScrollView view) {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                mPullDownScrollView.finishRefresh("上次刷新时间:12:23");
            }
        }, 2000);
    }
}
